package org.dromara.school.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.dromara.common.core.exception.ExcelException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.school.domain.Campus;
import org.dromara.school.domain.Discipline;
import org.dromara.school.domain.vo.DisciplineVo;
import org.dromara.school.service.ICampusService;
import org.dromara.school.service.IDisciplineService;
import java.util.*;

/**
 * @Description 学科管理批量导入
 * @Date 2023/11/3 11:28
 * @Author 郭建波
 **/
public class DisciplineImportExcelListener extends AnalysisEventListener<DisciplineVo> {


    private IDisciplineService idsciplineService;
    private ICampusService campusService;

    //定义存放数据的集合
    private List<Discipline> list = new ArrayList<>();

    public DisciplineImportExcelListener(IDisciplineService idsciplineService, ICampusService campusService) {
        this.idsciplineService = idsciplineService;
        this.campusService = campusService;
    }

    // 一行行读取内容
    @Override
    public void invoke(DisciplineVo disciplineVo, AnalysisContext analysisContext) {

        Campus campus = this.checkNull(disciplineVo, analysisContext);
        //读取每行数据，对数据进行判断以及处理

        Long campusId = campus.getCampusId();//得到分校id
        String subjectName = disciplineVo.getSubjectName();
        String grade = disciplineVo.getGradeName();//获取表格信息
        Discipline discipline = new Discipline();
        discipline.setSubjectName(subjectName);
        discipline.setSchoolCampusId(campusId);
        discipline.setGrade(grade.replaceAll("\\|", ","));
        //通过学科跟班级查看是否存在
        this.checkHashSubject(discipline, analysisContext);
        list.add(discipline);
    }


    //查看是否已经存在
    private void checkHashSubject(Discipline discipline, AnalysisContext context) {
        List<Discipline> disciplineList = idsciplineService.findDisciplineList(discipline);
        if (disciplineList.size() > 0) {
            int row = context.readRowHolder().getRowIndex() + 1;
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行 " + "学科:" + discipline.getSubjectName() + " 相同校区班级已存在！");
        }

    }


    // 读取表头内容
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    // 读取完成之后的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        list.forEach(discipline -> {
            this.idsciplineService.insertDisciplineByExcel(discipline);
        });

    }


    public Campus checkNull(DisciplineVo disciplineVo, AnalysisContext context) {
        int row = context.readRowHolder().getRowIndex() + 1;

        if (StringUtils.isEmpty(disciplineVo.getSubjectName()) || StringUtils.isEmpty(disciplineVo.getCampusName()) || StringUtils.isEmpty(disciplineVo.getGradeName())) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  出现导入必须要的数据为空,请检查！");
        }

        String campusName = disciplineVo.getCampusName();//拿到分校名称
        Campus campus = new Campus();
        campus.setCampusName(campusName);
        Campus getSchoolCampus = campusService.findSchoolCampus(campus);
        if (StringUtils.isNull(getSchoolCampus)) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的校区没有找到,请检查！");
        }
        return getSchoolCampus;


    }


}
